package com.situ.web.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.web.entity.City;
import com.situ.web.entity.Province;
import com.situ.web.entity.Student;
import com.situ.web.util.JDBCUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@WebServlet("/area")
public class AreaServlet extends HttpServlet{

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String method = req.getParameter("method");
		switch (method) {
		case "selectProvince":
			selectProvince(req, resp);
			break;
		case "selectCity":
			selectCity(req, resp);
			break;

		default:
			break;
		}
	}

	private void selectProvince(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		ArrayList<Province> list = new ArrayList<Province>();
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT id,province FROM tm_province";
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String provinceName = resultSet.getString("province");
				Province province = new Province(id, provinceName);
				list.add(province);
			}
			
			for (Province province : list) {
				System.out.println(province);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
		
		// 以json格式返回List<Province>
		JSONArray jsonArray = JSONArray.fromObject(list);
		resp.setContentType("text/html;charset=utf-8");
		resp.getWriter().write(jsonArray.toString());
		
	}
	
	private void selectCity(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String provinceId = req.getParameter("provinceId");
		
		ArrayList<City> list = new ArrayList<City>();
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT id,city FROM tm_city where province_id=?";
			statement = connection.prepareStatement(sql);
			statement.setInt(1, Integer.parseInt(provinceId));
			resultSet = statement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String cityName = resultSet.getString("city");
				City city = new City(id, cityName);
				list.add(city);
			}
			
			for (City city : list) {
				System.out.println(city);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
		
		// 以json格式返回List<Province>
		JSONArray jsonArray = JSONArray.fromObject(list);
		resp.setContentType("text/html;charset=utf-8");
		resp.getWriter().write(jsonArray.toString());
		
	}
	
	
	public static void main(String[] args) {
		Student student = new Student(1, "张三", 23, "男");
		JSONObject jsonObject =  JSONObject.fromObject(student);
		// {"age":23,"gender":"男","id":1,"name":"张三"}
		System.out.println(jsonObject.toString());
		
		List<Student> list = new ArrayList<Student>();
		Student student2 = new Student(2, "李四", 23, "男");
		list.add(student);
		list.add(student2);
		JSONArray jsonArray = JSONArray.fromObject(list);
		// [{"age":23,"gender":"男","id":1,"name":"张三"},{"age":23,"gender":"男","id":2,"name":"李四"}]
		System.out.println(jsonArray.toString());
	}
	
	
	
}
